package com.zut.edu.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;
import com.zut.edu.MainActivity;
import com.zut.edu.PersonVerifyActivity;
import com.zut.edu.R;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class StringUtils {
    public static final String BASE_URL="http://39.107.42.126:8088";

    public static boolean isEmpty(String str) {
        if (str == null || str.length() <= 0) {
            return true;
        } else {
            return false;
        }
    }

    public static String getLocalitem(Context context,String lable,String key){
        SharedPreferences sp = context.getSharedPreferences("nfc_pre",context.MODE_PRIVATE);
        String data = sp.getString(lable,"");
        if(StringUtils.isEmpty(data))return "null";
        JSONObject jsonObject = JSON.parseObject(data);
        String str = jsonObject.getString(key);
        return str;
    }

    public static String getLocalString(Context context,String lable){
        SharedPreferences sp = context.getSharedPreferences("nfc_pre",context.MODE_PRIVATE);
        String str = sp.getString(lable,"");
        return str;
    }

    public static boolean postLocalObject(Context context,Object value,String key){
        String data = JSONObject.toJSONString(value);
        SharedPreferences sp = context.getSharedPreferences("nfc_pre",context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key,data);
        return editor.commit();
    }

    public static boolean postLocalString(Context context,String value,String key){
        SharedPreferences sp = context.getSharedPreferences("nfc_pre",context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString(key,value);
        return editor.commit();
    }


    public static String statusJudge(String lable,String key){
        if(lable.equals("userStatus")){
            switch (key){
                case "0":return "已通过";
                case "2":return "已拒绝";
                default:return "待处理";

            }
        }
        else if(lable.equals("userIdentity")){
            switch (key){
                case "2":return "管理人员";
                default:return "操作人员";

            }
        }
        else if(lable.equals("eventStatus")){
            switch (key){
                case "1":return "成功";
                case "3":return "特别关注";
                default:return "失败";

            }
        }
        else if(lable.equals("tagStatus")){
            switch (key){
                case "1":return "正常";
                case "2":return "损坏";
                case "3":return "特别关注";
                default:return "未出厂";

            }
        }

        return "null";
    }

    public static int colorJudge(String lable,String key){
        if(lable.equals("userStatus")){
            switch (key){
                case "0":return R.color.qmui_config_color_gray_6;
                case "1":return R.color.app_color_theme_4;
                case "2":return R.color.app_color_theme_1;
                default:return R.color.qmui_config_color_white;

            }
        }

        else if(lable.equals("userIdentity")){
            switch (key){
                case "2":return R.color.app_color_theme_8;
                default:return R.color.app_color_theme_3;
            }
        }

        else if(lable.equals("eventStatus")){
            switch (key){
                case "1":return R.color.app_color_theme_4;
                case "3":return R.color.app_color_theme_1;
                default:return R.color.qmui_config_color_gray_6;
            }
        }
        else if(lable.equals("tagStatus")){
            switch (key){
                case "1":return R.color.app_color_theme_4;
                case "2":return R.color.qmui_config_color_gray_6;
                case "3":return R.color.app_color_theme_1;
                default:return R.color.app_color_blue;
            }
        }
        else if(lable.equals("randomColor")){
            int i = (int)key.charAt(0)%8+1;
            switch (i){
                case 1:return R.color.app_color_theme_1;
                case 2:return R.color.app_color_theme_2;
                case 3:return R.color.app_color_theme_3;
                case 4:return R.color.app_color_theme_4;
                case 5:return R.color.app_color_theme_5;
                case 6:return R.color.app_color_theme_6;
                case 7:return R.color.app_color_theme_7;
                case 8:return R.color.app_color_theme_8;
                default:return R.color.qmui_config_color_gray_6;
            }
        }
        return R.color.qmui_config_color_white;
    }

    public static String currentTime(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        String eventDate = simpleDateFormat.format(date);
        return eventDate;
    }

    public static void basicInstall(Context context){
        StringUtils.postLocalString(context,"0","lock");
        StringUtils.postLocalString(context,"0","shock");
        StringUtils.postLocalString(context,"0","volume");
    }

    public static void basicUninstall(Context context){
        SharedPreferences sp = context.getSharedPreferences("nfc_pre",context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.remove("user");
        editor.remove("lock");
        editor.remove("company");
        editor.commit();
    }

    public static void initShock(Context context){
        final Vibrator vibrator = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE);
        final long[] patter = {10, 500};
        vibrator.vibrate(patter, 0);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep( 500);  //线程休眠1秒执行
                    vibrator.cancel();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public static String MD5(String inStr) throws Exception {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }

        byte[] byteArray = inStr.getBytes("UTF-8");
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }

    public static void photoURL(Context context,String path,QMUIRadiusImageView view){
        if(!StringUtils.isEmpty(path)){
            Glide.with(context)
                    .load(path)
                    .into(view);
          }
        }
}
